package com.knowswift.issues.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.knowswift.common.bean.issue.po.MonitorDevice;
import com.knowswift.common.bean.issue.vo.MonitorDeviceCountVO;
import com.knowswift.common.bean.issue.vo.MonitorDeviceVO;
import com.knowswift.common.common.GlobalProperty;
import com.knowswift.common.common.Page;
import com.knowswift.common.enums.RoleEnum;
import com.knowswift.issues.mapper.MonitorDeviceMapper;
import com.knowswift.security.account.User;
import com.knowswift.security.account.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MonitorDeviceService extends ServiceImpl<MonitorDeviceMapper, MonitorDevice> {

//    public MonitorDeviceCountVO countByDevice(String moduleName, User user) {
//        LambdaQueryWrapper<MonitorDevice> allDeviceCountWrapper = new LambdaQueryWrapper<>();
//
//        LambdaQueryWrapper<MonitorDevice> alertDeviceCountWrapper = new LambdaQueryWrapper<>();
//        alertDeviceCountWrapper.eq(MonitorDevice::getDeviceStatus, GlobalProperty.DEVICE_STATUS_ALERT);
//
//        LambdaQueryWrapper<MonitorDevice> normalDeviceCountWrapper = new LambdaQueryWrapper<>();
//        normalDeviceCountWrapper.eq(MonitorDevice::getDeviceStatus, GlobalProperty.DEVICE_STATUS_NORMAL);
//
//        LambdaQueryWrapper<MonitorDevice> errorDeviceCountWrapper = new LambdaQueryWrapper<>();
//        errorDeviceCountWrapper.eq(MonitorDevice::getDeviceStatus, GlobalProperty.DEVICE_STATUS_ERROR);
//
//        LambdaQueryWrapper<MonitorDevice> deviceLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        if (!RoleEnum.isAdmin(user.getRole()) && !UserService.checkAuthority(user,"errorMonitor")){
//            deviceLambdaQueryWrapper.ne(MonitorDevice::getDeviceStatus, GlobalProperty.DEVICE_STATUS_ERROR);
//        }
//
//        if (!StringUtils.isBlank(moduleName)){
//            allDeviceCountWrapper.eq(MonitorDevice::getModuleName,moduleName);
//            alertDeviceCountWrapper.eq(MonitorDevice::getModuleName,moduleName);
//            normalDeviceCountWrapper.eq(MonitorDevice::getModuleName,moduleName);
//            errorDeviceCountWrapper.eq(MonitorDevice::getModuleName,moduleName);
//            deviceLambdaQueryWrapper.eq(MonitorDevice::getModuleName, moduleName);
//        }
//
//        Integer allDeviceCount = baseMapper.selectCount(allDeviceCountWrapper);
//        Integer alertDeviceCount = baseMapper.selectCount(alertDeviceCountWrapper);
//        Integer normalDeviceCount = baseMapper.selectCount(normalDeviceCountWrapper);
//        Integer errorDeviceCount = baseMapper.selectCount(errorDeviceCountWrapper);
//        List<MonitorDevice> monitorDevices = baseMapper.selectList(deviceLambdaQueryWrapper);
//
//        MonitorDeviceCountVO monitorDeviceCountVO = new MonitorDeviceCountVO();
//        monitorDeviceCountVO.setAllDeviceCount(allDeviceCount);
//        monitorDeviceCountVO.setAlertDeviceCount(alertDeviceCount);
//        monitorDeviceCountVO.setNormalDeviceCount(normalDeviceCount);
//        monitorDeviceCountVO.setErrorDeviceCount(errorDeviceCount);
//        monitorDeviceCountVO.setMonitorDevices(monitorDevices);
//
//        return monitorDeviceCountVO;
//    }

    public MonitorDeviceCountVO countDevice(String moduleName, String projectName, List<String> moduleProjects, User user) {
        Integer allDeviceCount = baseMapper.countDeviceByStatus(moduleName, projectName, moduleProjects, null);
        Integer alertDeviceCount = baseMapper.countDeviceByStatus(moduleName, projectName, moduleProjects, GlobalProperty.DEVICE_STATUS_ALERT);
        Integer normalDeviceCount = baseMapper.countDeviceByStatus(moduleName, projectName, moduleProjects, GlobalProperty.DEVICE_STATUS_NORMAL);
        Integer errorDeviceCount = baseMapper.countDeviceByStatus(moduleName, projectName, moduleProjects, GlobalProperty.DEVICE_STATUS_ERROR);

        MonitorDeviceCountVO monitorDeviceCountVO = new MonitorDeviceCountVO();
        monitorDeviceCountVO.setAllDeviceCount(allDeviceCount);
        monitorDeviceCountVO.setAlertDeviceCount(alertDeviceCount);
        monitorDeviceCountVO.setNormalDeviceCount(normalDeviceCount);
        monitorDeviceCountVO.setErrorDeviceCount(errorDeviceCount);
        monitorDeviceCountVO.setIsOrange(true);

//        Integer status = null;
        List<MonitorDevice> monitorDevices = baseMapper.monitorDeviceMap(moduleName, projectName, moduleProjects, null);
        if (!RoleEnum.isAdmin(user.getRole()) && !UserService.checkAuthority(user, "errorMonitor")) {
            monitorDeviceCountVO.setNormalDeviceCount(normalDeviceCount+errorDeviceCount);
            monitorDeviceCountVO.setIsOrange(false);
            monitorDeviceCountVO.setErrorDeviceCount(0);
//            status = GlobalProperty.DEVICE_STATUS_ERROR;

            for (MonitorDevice monitorDevice : monitorDevices) {
                if (GlobalProperty.DEVICE_STATUS_ERROR.equals(monitorDevice.getDeviceStatus())){
                    monitorDevice.setDeviceStatus(GlobalProperty.DEVICE_STATUS_NORMAL);
                }
            }
        }
        monitorDeviceCountVO.setMonitorDevices(monitorDevices);

        return monitorDeviceCountVO;
    }

    public Page<MonitorDevice> dropDownList(String name, MonitorDeviceVO param,List<String> moduleProjects, Page<MonitorDevice> page) {
        return baseMapper.dropDownList(name,param, moduleProjects,page);
    }
}
